﻿using Compass.Wasm.Shared.Data.Hoods;
using SolidWorks.Interop.sldworks;

namespace Compass.Wpf.BatchWorks.Hoods;
public interface IUvirAutoDrawing:IAutoDrawing
{

}
public class UvirAutoDrawing:BaseAutoDrawing,IUvirAutoDrawing
{
    #region ctor
    private readonly IUvirDataService _service;
    public UvirAutoDrawing(IContainerProvider provider):base(provider)
    {
        _service = provider.Resolve<IUvirDataService>();
    }

    #endregion

    public async Task AutoDrawingAsync(ModuleDto moduleDto)
    {
        try
        {
            #region 文件夹准备与打包，打开顶级装配体
            var dataResult = await _service.GetFirstOrDefault(moduleDto.Id.Value);
            var data = dataResult.Result;//获取制图数据
            //todo:检查模型moduleDto.ModelName，看是那种子类
            var modelPath = moduleDto.ModelName.GetModelPath();
            //优化进程外调用命令变缓慢的问题
            SwApp.CommandInProgress = true;
            //打包,后续需要使用到的变量：suffix，packPath
            var packPath = SwApp.PackToProject(out var suffix, modelPath, moduleDto, Aggregator);
            //顶级Model,顶级Assy,打开Pack后的模型packPath
            var swAssyTop = SwApp.OpenAssemblyDoc(out var swModelTop, packPath, Aggregator);
            #endregion

            switch (moduleDto.ModelName)
            {
                case "UVIR_555":
                    Uvir555(data, swModelTop, swAssyTop, suffix);
                    break;
            }

            #region 保存操作
            //设置成true，直接更新顶层，速度很快，设置成false，每个零件都会更新，很慢
            swModelTop.ForceRebuild3(true);
            swModelTop.Save(); //保存，很耗时间
            SwApp.CloseDoc(packPath); //关闭，很快
            #endregion
        }
        catch
        {
            SwApp.CommandInProgress = false;
            await Task.Delay(500);
            throw;
        }
        finally
        {
            SwApp.CommandInProgress = false;
        }
    }

    private void Uvir555(UviData data, ModelDoc2 swModelTop, AssemblyDoc swAssyTop, string suffix)
    {
        #region 计算中间值与顶层操作
        //计算烟罩净长度，计算烟罩净深度
        //根据直径计算烟罩长度
        var outerRadius = data.Length / 2d;
        var innerRadius = data.Length / 2d - 90d;
        var netLength =Math.Floor(2d*Math.Sqrt(Math.Pow(innerRadius, 2)-Math.Pow(400d,2)));

        //赋值为0时为均分一半，否则需要赋值
        var netMiddleToRight = data.MiddleToRight.Equals(0) ? netLength/2d : data.MiddleToRight;


        //烟罩宽度，考虑是否有BackCj
        //swModelTop.ChangeDim("Width@DistanceWidth", netWidth);
        #endregion
        #region  Exhaust_UV_M_555，UVM555排风装配
        ExhaustService.UvR555(swAssyTop, suffix, netLength, data.Height, data.SidePanel, data.UvLightType, netMiddleToRight, data.ExhaustSpigotNumber, data.ExhaustSpigotLength, data.ExhaustSpigotWidth, data.ExhaustSpigotHeight, data.ExhaustSpigotDis, data.DrainType, data.WaterCollection, data.Marvel, data.Ansul, data.AnsulSide, data.AnsulDetector, SupplyType_e.圆形);
        #endregion
        #region MidRoof_R,MidRoof装配
        MidRoofService.MidRoofR(swAssyTop, suffix,outerRadius,innerRadius, netLength,data.LightType, data.SpotLightNumber, data.SpotLightDistance, data.Marvel);
        #endregion
        #region Supply_I_R_555,IM555新风装配
        SupplyService.IR555(swAssyTop, suffix, outerRadius, innerRadius,netLength, data.Bluetooth,  data.WaterCollection);
        #endregion
    }
}